var express = require('express');
var router = express.Router();
const {uploadFiles} = require('../utils/handleFiles')

/* 图片上传 */
router.post('/uploadImage', function(req, res, next) {
    const uploadImages = uploadFiles({
        dir:'./public/temp'
        //key:'file',//和前端定义的file名字匹配上
        //size:1000//表示默认值kb
    });
    uploadImages(req,res,(error)=>{
        if(error){
            console.log("图片上传失败",error);
        }else{
            console.log("图片上传成功:",req.files);
            //输出上传成功后的图片信息
            res.send({
                message:'图片上传成功',
                code:200,
                data:'./temp/'+req.files[0].filename
            })
        }
    });
});

router.post('/uploadImages', function (req, res, next) {
    // 图片上传
    const uploadImages = uploadFiles({
        // 图片上传成功后的存储路径
        path: './public/assets',
        // key: 'file',
        // size: 1000
    });
    uploadImages(req, res, (error) => {
        if(error) {
            console.log('图片上传失败', error);
        } else {
            console.log('图片上传成功');
            // 输出上传成功后的图片信息
            console.log(req.files);
        }
    });
});

module.exports = router;